//pages/rank/rank.js
const common = require("../../../utils/zstz.js");
//获取应用实例
const app = getApp().globalData;

Page({
  /**
   * 页面的初始数据
   */
  data: {
    score: 0,
    todayRank: '',
    weekRank: '',
    today: [],
    week: [],
    currIndex: '0',
    currentShow: [],
    active: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.todayRank();
    this.weekRank();
    this.setData({
      score: options.score,
      // 排行榜的信息，
      headImg: wx.getStorageSync('userInfo').avatarUrl,
      name: wx.getStorageSync('userInfo').nickName
    })
  },

  //点击今日排名，本周排名 替换数据
  changeData(e) {
    let that = this,
      info = [];
    if (e.target.dataset.index == '0') {
      info = that.data.today;
    } else {
      info = that.data.week;
    }
    this.setData({
      currentShow: info,
      currIndex: e.target.dataset.index
    })
  },

  //今日排行榜接口
  todayRank() {
    let openId = wx.getStorageSync('openid');
    let token = wx.getStorageSync('token');
    let parameter = openId + '&token=' + token + '&studyFid=' + app.fid,
      that = this;
    wx.request({
      url: common.baseUrl + 'score/dayRank?openId=' + parameter,
      method: 'get',
      data: {
        fid: app.fid,
        uid: wx.getStorageSync('cxid'),
        wxType: app.wxTypes[11].id
      },
      header: {
        'content-type': 'application/json' //默认值
      },
      success: (res) => {
        if (res.data.success) {
          let arr = res.data.data,
            num;
          console.log(arr)
          if (arr.length == 0) {
            that.setData({
              todayRank: '--',
              active: ''
            });
            return;
          } else {
            that.setData({
              today: arr,
              todayRank: num,
              currentShow: arr,
              active: 'noData'
            });
            for (let index = 0; index < arr.length; index++) {
              if (openId == arr[index].openId) {
                num = index + 1;
                that.setData({
                  todayRank: num,
                });
                return;
              } else {
                if (arr.length < 100) {
                  num = '--';
                } else {
                  num = '100+';
                }
                that.setData({
                  todayRank: num,
                });
              }
            }

          }

        }
      }
    })
  },

  //本周排行
  weekRank() {
    let openId = wx.getStorageSync('openid');
    let token = wx.getStorageSync('token');
    let parameter = openId + '&token=' + token + '&studyFid=' + app.fid,
      that = this;
    wx.request({
      url: common.baseUrl + 'score/weekRank?openId=' + parameter,
      method: 'get',
      data: {
        fid: app.fid,
        uid: wx.getStorageSync('cxid'),
        wxType: app.wxTypes[11].id
      },
      header: {
        'content-type': 'application/json' //默认值
      },
      success: (res) => {
        if (res.data.success) {
          let arr = res.data.data,
            num;
          if (arr.length == 0) {
            that.setData({
              weekRank: '--',
              active: ''
            });
          } else {
            that.setData({
              week: arr,
              active: 'noData'
            })
            for (let index = 0; index < arr.length; index++) {
              if (openId == arr[index].openId) {
                num = index + 1;
                that.setData({
                  weekRank: num,
                })
                return;
              } else {
                if (arr.length < 100) {
                  num = '--';
                } else {
                  num = '100+';
                }
                that.setData({
                  weekRank: num,
                })
              }
            }
          }
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
  
})